package web.servlets;


import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.exceptions.MapException;


import ejb.manager.GenerateMapManager;

/**
 * Servlet implementation class ImageServlet
 */
@WebServlet(name = "MapImage", urlPatterns = { "/MapImage" })
public class MapServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@EJB
	private GenerateMapManager gmm;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MapServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		response.setContentType("image/png");
		byte[] map = null;
		try {
			map = gmm.getMap();
		} catch (MapException e) {
			map = e.getBlankMap();
		}
		
		try {
			response.getOutputStream().write(map);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
